package com.dehui.traders.service;

import android.util.Log;

import com.dehui.traders.bean.BaseListResponse;
import com.dehui.traders.bean.BaseResponse;
import com.google.gson.Gson;

import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.ToastUtils;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public abstract class CallListener<T> implements Callback<T> {

    public static final String TAG = "CallListener";

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        int code = response.code();
        if (code == 200){
            try {

                BaseResponse<T> baseResponse = (BaseResponse<T>) response.body();
                int status = baseResponse.status;
                _onResponse(response);
                if(status != 200){
                    _onResponseError(status);
                }

            }catch (Exception e){

                try {

                    BaseListResponse<T> baseResponse = (BaseListResponse<T>) response.body();
                    int status = baseResponse.status;
                    _onResponse(response);
                    if(status != 200){
                        _onResponseError(status);
                    }

                }catch (Exception e1){

                    _onResponse(response);

                }

            }

        }else{
            Log.e("Hqq","onResponse Code ==>" + code);
        }
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        if (t != null)
            KLog.e(TAG,t.getMessage());
        _onFailure();
    }

    public abstract void _onResponse(Response<T> response);

    public abstract void _onFailure();

    public void _onResponseError(int status){
        if (status == 101){
            ToastUtils.showShort("账号密码错误");
        }else if (status == 102){
            ToastUtils.showShort("验证码错误");
        }else if (status == 103){
            ToastUtils.showShort("验证码超时");
        }else if (status == 104){
            ToastUtils.showShort("验证码发送失败");
        }else if(status == 105){
            ToastUtils.showShort("用户未注册");
        }else if(status == 106){
            ToastUtils.showShort("密码输入错误");
        }else if(status == 107){
            ToastUtils.showShort("请勿重复注册");
        }else if(status == 210){
            ToastUtils.showShort("今日未开盘或停盘");
        }else if(status == 300){
            ToastUtils.showShort("数据库出错");
        }else if(status == 301){
            ToastUtils.showShort("数据库没有对应数据");
        }else if(status == 401){
            ToastUtils.showShort("python获取角本失败");
        }else if(status == 402){
            ToastUtils.showShort("开盘价为0");
        }else if(status == 500){
            ToastUtils.showShort("参数错误");
        }else if(status == 501){
            ToastUtils.showShort("header验证失败");
        }else if(status == 900){
            ToastUtils.showShort("系统错误");
        }else{
            ToastUtils.showShort("未知错误" + status);
        }


    }

}
